﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TaobaoDistrSales.Model;
using System.Windows.Forms;
using System.Data.Objects;

namespace TaobaoDistrSales.Common
{
    public class ResourceManager
    {
        #region Static Ctor

        static ResourceManager()
        {
            bool useTop = true;
            string useTopValue = ConfigurationManager.AppSettings[Constants.UseTop];
            if (!String.IsNullOrEmpty(useTopValue))
            {
                Boolean.TryParse(useTopValue, out useTop);
            }
            
            string urlKey = useTop ? Constants.TopUrl:Constants.SandBoxUrl;

            Current.RequestUrl = ConfigurationManager.AppSettings[urlKey];
            if (String.IsNullOrEmpty(Current.RequestUrl))
            {
                throw new DistrSalesException("系统没有配置好淘宝API的URL!");
            }

            Current.ConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings[Constants.ConnectionStrName]].ConnectionString;

            Current.LoadEntity();
        }

        protected ResourceManager()
        { 
        }

        #endregion

        #region Current

        public static readonly ResourceManager Current = new ResourceManager();

        #endregion

        #region Properties

        public string RequestUrl
        {
            get;
            private set;
        }

        protected string ConnectionString
        {
            get;
            private set;
        }
         
        public OptionsConfig OptionsConfig
        {
            get;
             set;
        }
        #endregion

        #region Load

        public taobaodbEntities LoadEntity()
        {
            taobaodbEntities entities = null;
            try
            {
                entities = new taobaodbEntities(Current.ConnectionString); 
                OptionsConfig = entities.OptionsConfig.FirstOrDefault();

                if (OptionsConfig == null)
                {
                    OptionsConfig = new OptionsConfig();
                    OptionsConfig.displaypanelnum = 1;
                    OptionsConfig.pagesize = 10;
                    OptionsConfig.enablefont = true;
                    OptionsConfig.openpanelatstart = false;
                    OptionsConfig.processinterval = 10;
                    OptionsConfig.clearoutput = true;
                    OptionsConfig.showlog = true;
                    OptionsConfig.showmax = false;
                    entities.SaveChanges();

                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return entities;
        }

        public void DisposeEntity(ObjectContext entity)
        {
            try
            {
                if (entity != null)
                {
                    entity.Dispose();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        #endregion
    }
}
